$(function(){
	resizeWidth()

	var nullStr = "-";
	var pageNo = 1;
	var dataCount = 20;
	var $userBetList = $("#userBetList");
	var trDetach = $userBetList.find("tr:first-of-type").detach();
	var seachControl = true;
	var sTvmid='',sTime='',tempTvmid,tempTime;

	//年份和月份的数据渲染
	var defyear = 2018;
	var defmonth = 1;
	for(var y=0;y<10;y++){
		var optionStr = "<option value="+defyear+">"+defyear+"年</option>";
		defyear ++;
		$("#select_year").append(optionStr);
	}
	
	for(var m=0;m<12;m++){
		var defmonthVal = defmonth;
		defmonthVal = defmonthVal<10?"0"+defmonthVal:defmonthVal;
		var optionStr = "<option value="+defmonthVal+">"+defmonth+"月</option>";
		defmonth ++;
		$("#select_month").append(optionStr);
	}

	//查询按钮
	$("#btn_search").on("click",function(){
		sTvmid = $("#search_tvmid").val();
		sTime = $("#select_year").val() + $("#select_month").val();
		if(sTvmid == ""){
			alert("请输入要查询的用户Tvmid");
			return;
		}
		$userBetList.empty();
		getBetData(pageNo);
	});

	//获取数据
	function getBetData($pageNo){
		if(!seachControl){
			return;
		}
		// seachControl = true;
		$.ajax({
			type:'get',
			dataType:'json',
			url:'/index.php?d=backstage&c=goldtimes/selecttvmid',
			data:{
				tvmid: sTvmid,
				time: sTime,
				page: $pageNo,
				count: dataCount
			},
			success:function(res){
				$userBetList.empty();
				$("#page").empty();
				$(".table_box").css("height","auto");
				$(".table_box .sequence_list").css("height","auto");
				if(res.code == '200'){
					if(!$.isEmptyObject(res.data)){
						renderBetList(res.data);
						renderPage(res.data.paging);
						InitTableHeight();
						// $("#download_btn").show();
					}else{
						noDataTipShow($userBetList);
						// $("#download_btn").hide();
					}
				}else if(res.code == '404'){
					noDataTipShow($userBetList,"抱歉，您还没有订单");
					// $("#download_btn").hide();
					// seachControl = false;
				}else{
					alert("参数错误");
					// $("#download_btn").hide();
				}
				
			},
			beforeSend:function(){
				noDataTipHide();
				loadingShow();
			},
			complete:function(){
				loadingHide()
			}
		});
	};

	//渲染数据表
	function renderBetList($data){
		var ListArr = $data.data;  
		var status="";
		for(var i in ListArr){
			var item = ListArr[i];
			var tr = trDetach.clone();
			tr.find(".oid").text(item.oid?item.oid:nullStr);
			tr.find(".second_num").text(item.second_num?item.second_num:nullStr);
			tr.find(".amount").text(item.amount?item.amount:nullStr);
			tr.find(".min_num").text(item.min_num?item.min_num:nullStr);
			tr.find(".max_num").text(item.max_num?item.max_num:nullStr);
			tr.find(".longtime").text(item.longtime?item.longtime:nullStr);
			tr.find(".createtime").text(item.createtime?item.createtime:nullStr);
			tr.appendTo($userBetList);
		}
	};

	//分页
	function renderPage(data){
		var totalPage = data.total ? Math.ceil(data.total/data.count): 1;
		var page = data.page ? data.page: 1;

		$("#page").createPage({
		    pageCount: totalPage,
	        current: page,
	        backFn: function(p) {
	            getBetData(p)
	        }
		})
	};

	//下载
	$("#userBetMain").on("click","#download_btn",function(){
		var downloadUrl = "/index.php?d=backstage&c=goldtimes/selectTvmidDown&time="+sTime+"&tvmid="+sTvmid;
		window.location.href= downloadUrl;
	});
});